#include "Light.h"

struct rt_device_pwm *pwm_dev;      /* PWM设备句柄 */

//pwm设备以指定设置开启
rt_err_t Light_open(void)
{
    rt_uint32_t period, pulse;
    rt_err_t result = RT_EOK;
    period = period_init;    /* 周期为0.5ms，单位为纳秒ns */
    //dir = dir_init;            /* PWM脉冲宽度值的增减方向 */
    pulse = pulse_init;          /* PWM脉冲宽度值，单位为纳秒ns */

    /* 查找设备 */
    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
    if (pwm_dev == RT_NULL)
    {
        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
        result=RT_ERROR;
        return result;
    }

    /* 设置PWM周期和脉冲宽度默认值 */
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
    /* 使能设备 */
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
    return result;
}

/*按照千分比调节灯光亮度*/
rt_err_t Light_set_lumen(rt_uint16_t lumen)
{
    rt_err_t result = RT_EOK;
    lumen=(lumen>1000)?1000:lumen;
    result=rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period_init, (rt_uint32_t)(((float)lumen/1000)*(float)period_init));
    return result;
}
/*按照脉冲宽度值调节灯光亮度,主要用于PID输出*/
rt_err_t Light_set_pwm(rt_int32_t pwm_pulse)
{
    rt_err_t result = RT_EOK;
    pwm_pulse+=((light_pulse_max-light_pulse_min)/2);//由于PID输出有负值故将数据的一半用作负值，真正赋值时再纠正过来即可
    pwm_pulse=(pwm_pulse>light_pulse_max)?light_pulse_max:pwm_pulse;
    pwm_pulse=(pwm_pulse<light_pulse_min)?light_pulse_min:pwm_pulse;
    result=rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period_init, (rt_uint32_t)pwm_pulse);
    return result;
}
//pwm设备关闭
rt_err_t Light_close(void)
{
    rt_err_t result = RT_EOK;
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period_init, 0);
    result=rt_pwm_disable(pwm_dev, PWM_DEV_CHANNEL);
    return result;
}
/*灯光测试线程*/
/*static void Light_text_entry(void *parameter)
{
    rt_uint16_t lumen=0;
    if(Light_open()!=RT_EOK)
        rt_kprintf("%s:Light_text_entry run failed! Light_open error!\n", PWM_DEV_NAME);
    for(lumen=0;lumen<1001;lumen++)
    {
        if(Light_set_lumen(lumen)!=RT_EOK)
        {
            rt_kprintf("%s:Light_text_entry run failed! Light_open error!:%d\n", PWM_DEV_NAME,lumen);
            return;
        }
        rt_thread_mdelay(10);
    }
    for(lumen=1000;lumen>5;lumen--)
    {
        if(Light_set_lumen(lumen)!=RT_EOK)
        {
            rt_kprintf("%s:Light_text_entry run failed! Light_open error!:%d\n", PWM_DEV_NAME,lumen);
            return;
        }
        rt_thread_mdelay(10);
    }
    if(Light_close()!=RT_EOK)
        rt_kprintf("%s:Light_text_entry run failed! Light_close error!\n", PWM_DEV_NAME);
}

static int Light_sample(void)
{
    rt_thread_t distance_thread;

    distance_thread = rt_thread_create("Light",
                                     Light_text_entry,
                                     RT_NULL,
                                     512,
                                     2,
                                     20);
    if (distance_thread != RT_NULL)
    {
        rt_thread_startup(distance_thread);
    }

    return RT_EOK;
}*/
/*灯光测试线程*/
//MSH_CMD_EXPORT(Light_sample , Light_txt_my);
